package cn.mall.poster.controller;

import cn.mall.poster.service.IPosterService;
import cn.mall.poster.domain.Poster;
import cn.mall.poster.query.PosterQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/poster" )
public class PosterController {
    @Autowired
    public IPosterService posterService;


    /**
     * 保存和修改公用的
     * @param poster 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Poster poster) {
        try {
            if(poster.getType()!=null){
                if ( poster.getId() != null)
                    posterService.update(poster);
                else
                    posterService.insert(poster);
                return AjaxResult.me();
            }else {
                return AjaxResult.me().setSuccess(false).setMessage("type不能空!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    public AjaxResult remove(@PathVariable("id" ) Long id) {
        try {
            posterService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
    */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            posterService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
    * 根据Id获取用户
    * @param id
    * @return
    */
    @GetMapping("/{id}" )
    public AjaxResult loadById(@PathVariable("id" ) Long id) {
        try {
            Poster poster =posterService.loadById(id);
            return AjaxResult.me().setResultObj(poster);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult loadAll() {

        try {
            List< Poster> list = posterService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult pageList(@RequestBody PosterQuery query) {
        try {
            PageList<Poster> pageList = posterService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }




    @GetMapping("/getluobopic" )
    public AjaxResult getluobopic() {
        try {
            List<String> strs = new ArrayList<>();
            List<Poster> posters =posterService.loadBylunbopic();
            for (Poster poster:posters) {
                strs.add(poster.getImgurl());
            }
            return AjaxResult.me().setResultObj(strs);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }

    @GetMapping("/getboard" )
    public AjaxResult getboard() {
        try {
            List<String> strs = new ArrayList<>();
            List<Poster> posters =posterService.getboard();
            for (Poster poster:posters) {
                strs.add(poster.getImgurl());
            }
            return AjaxResult.me().setResultObj(strs);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }
}
